package com.cy.service.impl;

import com.cy.entity.User;
import com.cy.mapper.UserMapper;
import com.cy.service.IUserService;
import com.cy.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    public UserMapper userMapper;
    @Override
    public void reg(User user) {
        /**
         * 1检测用户名是否被注册 判读结果集是否为空
         * 2数据补全
         * 3执行注册操作
         */
        User result=userMapper.findByUserName(user.getUsername());
        if(result!=null)
        {
            throw new UsernameDuplicatedException("用户名被占用");
        }
        //密码加密处理 （md5 串+密码+串） 加密三次
        String oldPassword = user.getPassword();
        //随机生成串
        String salt = UUID.randomUUID().toString().toUpperCase();
        //记录盐值
        user.setSalt(salt);
        String md5Password = getMD5Password(oldPassword, salt);
        user.setPassword(md5Password);

        //数据补全操作 is_delete=0 四个日志信息
        user.setIsDelete(0);
        user.setCreatedUser(user.getUsername());
        Date date=new Date();
        user.setCreatedTime(date);
        user.setModifiedUser(user.getUsername());
        user.setModifiedTime(date);

        //执行插入操作
        Integer rows = userMapper.insert(user);
        if(rows!=1)
        {
            throw new InsertException("插入过程产生错误");
        }
    }

    @Override
    public User login(String username, String password) {
        User result = userMapper.findByUserName(username);
        if(result==null)
        {
            throw new UserNotFoundException("用户不存在");
        }
        //检测用户的密码
        String oldPassword=result.getPassword();
        String salt=result.getSalt();
        String newMd5Password=getMD5Password(password,salt);
        if(!newMd5Password.equals(oldPassword))
        {
            throw new PasswordNotMatchException("用户密码错误");
        }

        //判断是否有这个结果
        if(result.getIsDelete()==1)
        {
            throw new UserNotFoundException("用户不存在");
        }

       // User user=userMapper.findByUserName(username);
        //辅助其它界面 提升性能
        User user=new User();
        user.setUid(result.getUid());
        user.setUsername(result.getUsername());
        user.setAvatar(result.getAvatar());
        return  user;
    }

    @Override
    public void changePassword(Integer uid, String username, String oldPassword, String newPassword) {
        User result = userMapper.findByUid(uid);
        if(result==null||result.getIsDelete()==1){
            throw new UserNotFoundException("用户数据找不到");
        }

        String oldMd5Password = getMD5Password(oldPassword, result.getSalt());
        if(result.getPassword().equals(oldPassword))
        {
            throw new PasswordNotMatchException("密码不匹配");
        }
        //设置新密码,加密新密码然后更新
        String newMd5Password = getMD5Password(newPassword, result.getSalt());
        Integer rows = userMapper.updatePasswordByUid(uid, newMd5Password, username, new Date());

        if(rows!=1)
        {
            throw new UpdateException("更新数据产生未知异常");
        }
    }

    @Override
    public User getByUid(Integer uid) {
        User result = userMapper.findByUid(uid);
        if(result==null||result.getIsDelete()==1)
        {
            throw new UserNotFoundException("用户数据不存在");
        }

        User user=new User();
        user.setUsername(result.getUsername());
        user.setPhone(result.getPhone());
        user.setEmail(result.getEmail());
        user.setGender(result.getGender());

        return user;
    }

    @Override
    public void changeInfo(Integer uid, String username, User user) {
        User result = userMapper.findByUid(uid);

        if(result==null||result.getIsDelete()==1)
        {
            throw new UserNotFoundException("用户数据不存在");
        }

        user.setUid(uid);
        //user.setUsername(username);
        user.setModifiedUser(username);
        user.setModifiedTime(new Date());
        Integer rows = userMapper.updateInfoByUid(user);

        if(rows!=1)
        {
            throw new UpdateException("更新时异常");
        }

    }

    @Override
    public void changeAvatar(Integer uid, String avatar, String username) {
        //查询当前用户是否存在
        User result = userMapper.findByUid(uid);

        if(result==null||result.getIsDelete()==1)
        {
            throw new UserNotFoundException("用户数据不存在");
        }

        Integer rows = userMapper.updateAvatarByUid(uid, avatar, username,new Date());
        if(rows!=1)
        {
            throw new UpdateException("更新用户头像产生未知异常");
        }

    }

    private String getMD5Password(String password,String salt)
    {
        //md5加密算法
        for(int i=0;i<3;i++) {
            password=DigestUtils.md5DigestAsHex((salt + password + salt).getBytes());
        }

        return password.toString();
    }
}
